# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import ttk
from businessmen import *
from jiaocai import *
from students import *
from acceptance import *
from order import *
import businessmendb
import jiaocaidb
import studentsdb
import acceptancedb
import orderdb

class BusinessmenFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master  # 保存主窗口引用
        self.status = tk.StringVar()
        self.button_frame = tk.Frame(self)
        self.button_frame.pack(side=tk.BOTTOM, pady=20)  # 确保按钮在底部
        self.button_frame.pack_propagate(False)
        self.create_page()  # 调用方法创建页面内容
        self.create_tree_view()  # 调用方法创建表格视图
        self.show_data_in_treeview()  # 显示数据到表格

    def create_page(self):
        # 创建一个框架来放置表格和滚动条，并填充整个 InputFrame
        self.tree_frame = tk.Frame(self)
        self.tree_frame.pack(fill=tk.BOTH, expand=True)

        # 创建一个底部框架用于放置按钮和状态标签
        bottom_frame = tk.Frame(self)
        bottom_frame.pack(side=tk.BOTTOM, fill=tk.X)

        # 添加按钮到按钮框架
        button_frame = tk.Frame(bottom_frame)
        button_frame.pack(side=tk.RIGHT, fill=tk.X, expand=True)  # 按钮框架填充底部框架

        # 添加按钮
        tk.Button(button_frame, text='刷新数据', command=self.refresh_data).pack(side=tk.RIGHT, padx=10, pady=10)
        tk.Button(button_frame, text='修改书商信息', command=self.goto_BusinessmenChange_page).pack(side=tk.RIGHT, padx=10,
                                                                                              pady=10)
        tk.Button(button_frame, text='查询书商信息', command=self.goto_BusinessmenQuery_page).pack(side=tk.RIGHT, padx=10,
                                                                                             pady=10)
        tk.Button(button_frame, text='录入书商信息', command=self.goto_BusinessmenInput_page).pack(side=tk.RIGHT, padx=10,
                                                                                             pady=10)
        tk.Label(self, textvariable=self.status, padx=10, pady=10).pack(side=tk.RIGHT)

        # 确保底部框架在窗口大小变化时重新计算布局
        self.bind("<Configure>", self.on_resize)
        self.on_resize()  # 初始调用以设置正确的布局

    def on_resize(self, event=None):
        # 更新窗口布局，确保底部框架和状态标签位置正确
        self.update_idletasks()

    def create_tree_view(self):
        # 表格
        columns = ("bno", "bname", "pn")  # 定义列
        self.tree_view = ttk.Treeview(self.tree_frame, show="headings", columns=columns)  # 创建 Treeview 表格
        self.tree_view.column('bno', width=100, anchor='center')  # 设置列属性
        self.tree_view.column('bname', width=150, anchor='center')
        self.tree_view.column('pn', width=150, anchor='center')
        self.tree_view.heading('bno', text='书商号')  # 设置列标题
        self.tree_view.heading('bname', text='书商名')
        self.tree_view.heading('pn', text='联系电话')
        self.tree_view.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)  # 使用 pack 布局将表格填满整个 tree_frame 左侧

        # 添加垂直滚动条
        vsb = ttk.Scrollbar(self.tree_frame, orient="vertical", command=self.tree_view.yview)  # 创建垂直滚动条
        vsb.pack(side=tk.RIGHT, fill=tk.Y)  # 使用 pack 布局将滚动条放置在右侧
        self.tree_view.configure(yscrollcommand=vsb.set)  # 将滚动条与表格视图关联

    def show_data_in_treeview(self):
        businessmen = businessmendb.all()  # 获取书商数据
        for bm in businessmen:
            self.tree_view.insert('', tk.END, values=(bm['bno'], bm['bname'], bm['pn']))

    def refresh_data(self):
        for item in self.tree_view.get_children():
            self.tree_view.delete(item)
        self.show_data_in_treeview()
        self.status.set('数据已刷新')

    def goto_BusinessmenInput_page(self):
        # 跳转到录入书商信息界面
        self.BusinessmenInput_page = BusinessmenInputFrame(self.root)


    def goto_BusinessmenQuery_page(self):
        # 跳转到查询书商信息界面
        self.BusinessmenQuery_page = BusinessmenQueryFrame(self.root)


    def goto_BusinessmenChange_page(self):
        # 跳转到修改书商信息界面
        self.BusinessmenChange_page = BusinessmenChangeFrame(self.root)



class JiaocaiFrame(tk.Frame):  # 继承Frame类
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master  # 保存主窗口引用
        self.status = tk.StringVar()
        self.create_page()  # 调用方法创建页面内容
        self.create_tree_view()  # 调用方法创建表格视图
        self.show_data_in_treeview()  # 显示数据到表格

    def create_page(self):
        # 创建一个框架来放置表格和滚动条，并填充整个 InputFrame
        self.tree_frame = tk.Frame(self)
        self.tree_frame.pack(fill=tk.BOTH, expand=True)

        # 添加录入、查询、修改、刷新按钮和状态标签
        button_frame = tk.Frame(self)
        button_frame.pack(side=tk.BOTTOM, fill=tk.X, anchor='se')

        tk.Label(self, textvariable=self.status).pack(side=tk.BOTTOM, anchor=tk.SE, pady=10, padx=10)

        tk.Button(button_frame, text='刷新数据', command=self.refresh_data).pack(side=tk.RIGHT, padx=10, pady=10)
        tk.Button(button_frame, text='修改教材信息', command=self.goto_JiaocaiChange_page).pack(side=tk.RIGHT, padx=10, pady=10)
        tk.Button(button_frame, text='查询教材信息', command=self.goto_JiaocaiQuery_page).pack(side=tk.RIGHT, padx=10, pady=10)
        tk.Button(button_frame, text='录入教材信息', command=self.goto_JiaocaiInput_page).pack(side=tk.RIGHT, padx=10, pady=10)

    def create_tree_view(self):
        # 表格
        columns = ("ISBN", "jname", "jauthor", "price", "stock")  # 定义列
        self.tree_view = ttk.Treeview(self.tree_frame, show="headings", columns=columns)  # 创建 Treeview 表格
        self.tree_view.column('ISBN', width=100, anchor='center')  # 设置列属性
        self.tree_view.column('jname', width=250, anchor='center')
        self.tree_view.column('jauthor', width=250, anchor='center')
        self.tree_view.column('price', width=100, anchor='center')
        self.tree_view.column('stock', width=100, anchor='center')
        self.tree_view.heading('ISBN', text='ISBN')  # 设置列标题
        self.tree_view.heading('jname', text='教材名')
        self.tree_view.heading('jauthor', text='作者')
        self.tree_view.heading('price', text='单价')
        self.tree_view.heading('stock', text='库存')
        self.tree_view.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)  # 使用 pack 布局将表格填满整个 tree_frame 左侧

        # 添加垂直滚动条
        vsb = ttk.Scrollbar(self.tree_frame, orient="vertical", command=self.tree_view.yview)  # 创建垂直滚动条
        vsb.pack(side=tk.RIGHT, fill=tk.Y)  # 使用 pack 布局将滚动条放置在右侧
        self.tree_view.configure(yscrollcommand=vsb.set)  # 将滚动条与表格视图关联

    def show_data_in_treeview(self):
        jiaocai = jiaocaidb.all()  # 获取教材数据
        for bm in jiaocai:
            self.tree_view.insert('', tk.END, values=(bm['ISBN'], bm['jname'], bm['jauthor'], bm['price'], bm['stock']))

    def refresh_data(self):
        for item in self.tree_view.get_children():
            self.tree_view.delete(item)
        self.show_data_in_treeview()
        self.status.set('数据已刷新')

    def goto_JiaocaiInput_page(self):
        # 跳转到录入教材信息界面
        self.JiaocaiInput_page = JiaocaiInputFrame(self.root)


    def goto_JiaocaiQuery_page(self):
        # 跳转到查询教材信息界面
        self.JiaocaiQuery_page = JiaocaiQueryFrame(self.root)


    def goto_JiaocaiChange_page(self):
        # 跳转到修改教材信息界面
        self.JiaocaiChange_page = JiaocaiChangeFrame(self.root)

class StudentsFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master  # 保存主窗口引用
        self.status = tk.StringVar()
        self.button_frame = tk.Frame(self)
        self.button_frame.pack(side=tk.BOTTOM, pady=20)  # 确保按钮在底部
        self.button_frame.pack_propagate(False)
        self.create_page()  # 调用方法创建页面内容
        self.create_tree_view()  # 调用方法创建表格视图
        self.show_data_in_treeview()  # 显示数据到表格

    def create_page(self):
        # 创建一个框架来放置表格和滚动条，并填充整个 InputFrame
        self.tree_frame = tk.Frame(self)
        self.tree_frame.pack(fill=tk.BOTH, expand=True)

        # 创建一个底部框架用于放置按钮和状态标签
        bottom_frame = tk.Frame(self)
        bottom_frame.pack(side=tk.BOTTOM, fill=tk.X)

        # 添加按钮到按钮框架
        button_frame = tk.Frame(bottom_frame)
        button_frame.pack(side=tk.RIGHT, fill=tk.X, expand=True)  # 按钮框架填充底部框架

        # 添加按钮
        tk.Button(button_frame, text='刷新数据', command=self.refresh_data).pack(side=tk.RIGHT, padx=10, pady=10)
        tk.Button(button_frame, text='修改学生信息', command=self.goto_StudentsChange_page).pack(side=tk.RIGHT, padx=10,
                                                                                              pady=10)
        tk.Button(button_frame, text='查询学生信息', command=self.goto_StudentsQuery_page).pack(side=tk.RIGHT, padx=10,
                                                                                             pady=10)
        tk.Button(button_frame, text='录入学生信息', command=self.goto_StudentsInput_page).pack(side=tk.RIGHT, padx=10,
                                                                                             pady=10)
        tk.Label(self, textvariable=self.status, padx=10, pady=10).pack(side=tk.RIGHT)

        # 确保底部框架在窗口大小变化时重新计算布局
        self.bind("<Configure>", self.on_resize)
        self.on_resize()  # 初始调用以设置正确的布局

    def on_resize(self, event=None):
        # 更新窗口布局，确保底部框架和状态标签位置正确
        self.update_idletasks()

    def create_tree_view(self):
        # 表格
        columns = ("sno", "sname", "subject")  # 定义列
        self.tree_view = ttk.Treeview(self.tree_frame, show="headings", columns=columns)  # 创建 Treeview 表格
        self.tree_view.column('sno', width=100, anchor='center')  # 设置列属性
        self.tree_view.column('sname', width=150, anchor='center')
        self.tree_view.column('subject', width=150, anchor='center')
        self.tree_view.heading('sno', text='学号')  # 设置列标题
        self.tree_view.heading('sname', text='学生名')
        self.tree_view.heading('subject', text='专业')
        self.tree_view.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)  # 使用 pack 布局将表格填满整个 tree_frame 左侧

        # 添加垂直滚动条
        vsb = ttk.Scrollbar(self.tree_frame, orient="vertical", command=self.tree_view.yview)  # 创建垂直滚动条
        vsb.pack(side=tk.RIGHT, fill=tk.Y)  # 使用 pack 布局将滚动条放置在右侧
        self.tree_view.configure(yscrollcommand=vsb.set)  # 将滚动条与表格视图关联

    def show_data_in_treeview(self):
        students = studentsdb.all()  # 获取学生数据
        for bm in students:
            self.tree_view.insert('', tk.END, values=(bm['sno'], bm['sname'], bm['subject']))

    def refresh_data(self):
        for item in self.tree_view.get_children():
            self.tree_view.delete(item)
        self.show_data_in_treeview()
        self.status.set('数据已刷新')

    def goto_StudentsInput_page(self):
        # 跳转到录入学生信息界面
        self.StudentsInput_page = StudentsInputFrame(self.root)


    def goto_StudentsQuery_page(self):
        # 跳转到查询学生信息界面
        self.StudentsQuery_page = StudentsQueryFrame(self.root)


    def goto_StudentsChange_page(self):
        # 跳转到修改学生信息界面
        self.StudentsChange_page = StudentsChangeFrame(self.root)


class OrderFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master  # 保存主窗口引用
        self.status = tk.StringVar()
        self.create_page()  # 调用方法创建页面内容
        self.create_tree_view()  # 调用方法创建表格视图
        self.show_data_in_treeview()  # 显示数据到表格

    def create_page(self):
        # 创建一个框架来放置表格和滚动条，并填充整个 OrderFrame
        self.tree_frame = tk.Frame(self)
        self.tree_frame.pack(fill=tk.BOTH, expand=True)

        # 添加录入、查询、修改、删除、刷新按钮和状态标签
        button_frame = tk.Frame(self)
        button_frame.pack(side=tk.BOTTOM, fill=tk.X, anchor='se')

        tk.Label(self, textvariable=self.status).pack(side=tk.BOTTOM, anchor=tk.SE, pady=10, padx=10)

        tk.Button(button_frame, text='刷新数据', command=self.refresh_data).pack(side=tk.RIGHT, padx=10, pady=10)
        tk.Button(button_frame, text='修改订购信息', command=self.goto_OrderChange_page).pack(side=tk.RIGHT, padx=10, pady=10)
        tk.Button(button_frame, text='删除订购信息', command=self.goto_OrderDelete_page).pack(side=tk.RIGHT, padx=10, pady=10)
        tk.Button(button_frame, text='查询订购信息', command=self.goto_OrderQuery_page).pack(side=tk.RIGHT, padx=10, pady=10)
        tk.Button(button_frame, text='录入订购信息', command=self.goto_OrderInput_page).pack(side=tk.RIGHT, padx=10, pady=10)

    def create_tree_view(self):
        # 表格
        columns = ("ISBN", "aid", "bno", "quantity", "amount", "order time", "pay time")  # 定义列
        self.tree_view = ttk.Treeview(self.tree_frame, show="headings", columns=columns)  # 创建 Treeview 表格
        self.tree_view.column('ISBN', width=100, anchor='center')  # 设置列属性
        self.tree_view.column('aid', width=100, anchor='center')
        self.tree_view.column('bno', width=50, anchor='center')
        self.tree_view.column('quantity', width=100, anchor='center')
        self.tree_view.column('amount', width=100, anchor='center')
        self.tree_view.column('order time', width=110, anchor='center')
        self.tree_view.column('pay time', width=110, anchor='center')
        self.tree_view.heading('ISBN', text='ISBN')  # 设置列标题
        self.tree_view.heading('aid', text='职工号')
        self.tree_view.heading('bno', text='书商号')
        self.tree_view.heading('quantity', text='订购数量')
        self.tree_view.heading('amount', text='订购金额')
        self.tree_view.heading('order time', text='订购时间')
        self.tree_view.heading('pay time', text='付款时间')
        self.tree_view.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)  # 使用 pack 布局将表格填满整个 tree_frame 左侧

        # 添加垂直滚动条
        vsb = ttk.Scrollbar(self.tree_frame, orient="vertical", command=self.tree_view.yview)  # 创建垂直滚动条
        vsb.pack(side=tk.RIGHT, fill=tk.Y)  # 使用 pack 布局将滚动条放置在右侧
        self.tree_view.configure(yscrollcommand=vsb.set)  # 将滚动条与表格视图关联

    def show_data_in_treeview(self):
        order = orderdb.all()  # 获取订购数据
        for bm in order:
            self.tree_view.insert('', tk.END, values=(bm['ISBN'], bm['aid'], bm['bno'], bm['quantity'], bm['amount'], bm['order time'], bm['pay time']))

    def refresh_data(self):
        for item in self.tree_view.get_children():
            self.tree_view.delete(item)
        self.show_data_in_treeview()
        self.status.set('数据已刷新')

    def goto_OrderInput_page(self):
        # 跳转到录入订购信息界面
        self.OrderInput_page = OrderInputFrame(self.root)

    def goto_OrderQuery_page(self):
        # 跳转到查询订购信息界面
        self.OrderQuery_page = OrderQueryFrame(self.root)

    def goto_OrderDelete_page(self):
        # 跳转到删除订购信息界面
        selected_item = self.tree_view.selection()
        if selected_item:
            item = self.tree_view.item(selected_item)
            values = item['values']
            ISBN = values[0]
            aid = values[1]
            bno = values[2]
            order_time = values[5]
            pay_time = values[6]
            self.OrderDelete_page = OrderDeleteFrame(self.master, self.tree_view, selected_item, ISBN, aid, bno, pay_time, order_time)
        else:
            self.status.set('请先选择要删除的订购信息')

    def goto_OrderChange_page(self):
        # 跳转到修改订购信息界面
        self.OrderChange_page = OrderChangeFrame(self.root)



class AcceptanceFrame(tk.Frame):  # 继承Frame类
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master  # 保存主窗口引用
        self.status = tk.StringVar()
        self.create_page()  # 调用方法创建页面内容
        self.create_tree_view()  # 调用方法创建表格视图
        self.show_data_in_treeview()  # 显示数据到表格

    def create_page(self):
        # 创建一个框架来放置表格和滚动条，并填充整个 InputFrame
        self.tree_frame = tk.Frame(self)
        self.tree_frame.pack(fill=tk.BOTH, expand=True)

        # 添加录入、查询、修改、刷新按钮和状态标签
        button_frame = tk.Frame(self)
        button_frame.pack(side=tk.BOTTOM, fill=tk.X, anchor='se')

        tk.Label(self, textvariable=self.status).pack(side=tk.BOTTOM, anchor=tk.SE, pady=10, padx=10)

        tk.Button(button_frame, text='刷新数据', command=self.refresh_data).pack(side=tk.RIGHT, padx=10, pady=10)
        tk.Button(button_frame, text='修改领取信息', command=self.goto_AcceptanceChange_page).pack(side=tk.RIGHT, padx=10, pady=10)
        tk.Button(button_frame, text='删除领取信息', command=self.goto_AcceptanceDelete_page).pack(side=tk.RIGHT, padx=10,
                                                                                            pady=10)
        tk.Button(button_frame, text='查询领取信息', command=self.goto_AcceptanceQuery_page).pack(side=tk.RIGHT, padx=10, pady=10)
        tk.Button(button_frame, text='录入领取信息', command=self.goto_AcceptanceInput_page).pack(side=tk.RIGHT, padx=10, pady=10)

    def create_tree_view(self):
        # 表格
        columns = ("ISBN", "aid", "sno", "number", "time")  # 定义列
        self.tree_view = ttk.Treeview(self.tree_frame, show="headings", columns=columns)  # 创建 Treeview 表格
        self.tree_view.column('ISBN', width=100, anchor='center')  # 设置列属性
        self.tree_view.column('aid', width=100, anchor='center')
        self.tree_view.column('sno', width=100, anchor='center')
        self.tree_view.column('number', width=100, anchor='center')
        self.tree_view.column('time', width=100, anchor='center')
        self.tree_view.heading('ISBN', text='ISBN')  # 设置列标题
        self.tree_view.heading('aid', text='职工号')
        self.tree_view.heading('sno', text='学号')
        self.tree_view.heading('number', text='领取数量')
        self.tree_view.heading('time', text='领取时间')
        self.tree_view.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)  # 使用 pack 布局将表格填满整个 tree_frame 左侧

        # 添加垂直滚动条
        vsb = ttk.Scrollbar(self.tree_frame, orient="vertical", command=self.tree_view.yview)  # 创建垂直滚动条
        vsb.pack(side=tk.RIGHT, fill=tk.Y)  # 使用 pack 布局将滚动条放置在右侧
        self.tree_view.configure(yscrollcommand=vsb.set)  # 将滚动条与表格视图关联

    def show_data_in_treeview(self):
        acceptance = acceptancedb.all()  # 获取领取数据
        for bm in acceptance:
            self.tree_view.insert('', tk.END, values=(bm['ISBN'], bm['aid'], bm['sno'], bm['number'], bm['time']))

    def refresh_data(self):
        for item in self.tree_view.get_children():
            self.tree_view.delete(item)
        self.show_data_in_treeview()
        self.status.set('数据已刷新')

    def goto_AcceptanceInput_page(self):
        # 跳转到录入领取信息界面
        self.AcceptanceInput_page = AcceptanceInputFrame(self.root)

    def goto_AcceptanceQuery_page(self):
        # 跳转到查询领取信息界面
        self.AcceptanceQuery_page = AcceptanceQueryFrame(self.root)

    def goto_AcceptanceDelete_page(self):
        # 跳转到修改领取信息界面
        selected_item = self.tree_view.selection()
        if selected_item:
            item = self.tree_view.item(selected_item)
            values = item['values']
            ISBN, aid, sno, time = values[0], values[1], values[2], values[4]
            self.AcceptanceDelete_page = AcceptanceDeleteFrame(self.master, self.tree_view, selected_item, ISBN, aid, sno,time)
        else:
            self.status.set('请先选择要删除的订购信息')
    def goto_AcceptanceChange_page(self):
        # 跳转到修改领取信息界面
        self.AcceptanceChange_page = AcceptanceChangeFrame(self.root)

